<template>
  <div>
    <div class="head">
      <el-row :gutter="8">
        <el-col :span="2" :offset="0">
          <span style="line-height: 40px;">目标组织:</span>
        </el-col>
        <el-col :span="3" :offset="0">
          <el-button
            type="info"
            @click="temp_Open_Choice_Box"
          >{{team_choice_item == ''? '选择组织' : team_choice_item}}</el-button>
        </el-col>
      </el-row>
    </div>
    <file-list
      v-if="team_files_refresh"
      v-model="team_choice_item"
      :team_role="team_role"
      :team_choice_item_allow_write="team_choice_item_allow_write"
      style="height: 50vh;"
    />
    <el-dialog title="选择组织" :visible.sync="team_choice_box">
      <el-tree :data="team_choice_data" @node-click="team_Item_Choice" :default-expand-all="true"></el-tree>
    </el-dialog>
  </div>
</template>

<script>
import file_list from '../pan/file_list'
import { findByUserName } from '@/api/requests/team'
import { findByUserNameIsAdmin } from '@//api/requests/college'

export default {
  components: {
    'file-list': file_list
  },
  data () {
    return {
      team_list_data: [],
      team_choice_item: '软件',
      team_choice_item_allow_write: true,
      team_choice_box: false,
      team_choice_data: [],
      team_files_refresh: true,

    }
  },
  watch: {
    team_choice_item (val) {
      localStorage.setItem('team', val)
      if (val.indexOf('学院') != -1) {
        this.user_Is_College_Permission(val)
      } else {
        this.team_list_data.forEach(item => {
          if (val == item.teamName) {
            this.team_choice_item_allow_write = item.spaceWrite == 0 ? true : false
            this.team_role = item.teamRole
          }
        })
      }
    },
    team_choice_item_allow_write (val) {
      if (!val) {
        this.$message('注意,您不具有「增加/删除/修改」的权限！');
      }
    }
  },
  created () {
    findByUserName().then(res => {
      this.team_list_data = res.data
      this.team_Get_Choice_Data(this.team_list_data)
      let record = localStorage.getItem('team')
      if (record != undefined) {
        this.team_choice_item = record
      } else {
        this.team_choice_box = true
      }
    })
  },
  methods: {
    team_Item_Choice (val) {
      this.team_choice_item = val.label
      this.team_choice_box = false
    },
    team_Get_Choice_Data (val) {
      let data = []
      val.forEach(item => {
        let flag = true
        for (let i = 0; i < data.length; i++) {
          let data_item = data[i]
          if (data_item.label == item.collegeName) {
            flag = false
            data_item.children.push({
              label: item.teamName
            })
          }
        }
        if (flag) {
          data.push({
            label: item.collegeName,
            children: [{
              label: item.teamName
            }]
          })
        }
      })
      this.team_choice_data = data

    },
    temp_Open_Choice_Box () {
      this.team_choice_box = true
    },
    user_Is_College_Permission (val) {
      findByUserNameIsAdmin(val).then(res => {
        this.team_choice_item_allow_write = res.data
        // this.team_files_refresh = false
        // this.$nextTick(() => {
        //   this.team_files_refresh = true
        // })
      })
    }

  },
}
</script>      
<style lang="scss" scoped>
.head {
    width: 100vw;
    padding: 10px 5px 0 50px;
    float: left;
    position: absolute;
    top: 61px;
    left: 473px;
}
</style>             